package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.TestApiUtil;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.PayFinishEvent;
import com.asktgapp.eventbus.PutQuestionsChooseEvent;
import com.asktgapp.model.PutQuestionsChooseVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.user.activity.PutQuestionsChooseBrandActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//提问选择设备类型
public class PutQuestionsChooseTypeFragment extends BaseFragment {

    private BaseAdapter mTypeAdapter;
    private boolean isPutQuestions;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_put_questions_type, container, false);
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void a(PayFinishEvent b) {
        if (b.getType()) {
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
    }
    public static PutQuestionsChooseTypeFragment newInstance(boolean isPutQuestions) {
        PutQuestionsChooseTypeFragment fragment = new PutQuestionsChooseTypeFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isPutQuestions", isPutQuestions);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        Bundle bundle = getArguments();
        isPutQuestions = bundle.getBoolean("isPutQuestions");
        RecyclerView recyclerView = rootView.getRootView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));

        mTypeAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_put_questions_cohoose, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof PutQuestionsChooseVO) {
                            PutQuestionsChooseVO bean = (PutQuestionsChooseVO) data;
                            holder.setText(R.id.content, bean.getTypeName());
                        }

                    }

                    @Override
                    public void onItemClick(View view, int position) {
                        PutQuestionsChooseVO bean = (PutQuestionsChooseVO) mTypeAdapter.getData().get(position);
                        Intent intent = new Intent(getActivity(), PutQuestionsChooseBrandActivity.class);
                        bean.setTypeId(bean.getId());
                        bean.setPutQuestion(isPutQuestions);
//                        if(!isPutQuestions){
                            PutQuestionsChooseEvent.postEvent(bean);
//                        }else {
                            intent.putExtra("data", bean);
                            startActivity(intent);
//                        }

//                        getBaseActivity().onBackPressed();
                    }
                });
                return holder;
            }
        };
        recyclerView.setAdapter(mTypeAdapter);
    }


    @Override
    public void loadData() {

        getBrandLlist();
    }

    private void getBrandLlist() {
        Apiservice apiservice = TestApiUtil.getInstance().create();
        HashMap params = new HashMap();
//        params.put("typeId", typeId);
        Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call = apiservice.getMachineType();
        call.enqueue(new Callback<ApiResponseBody<List<PutQuestionsChooseVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Response<ApiResponseBody<List<PutQuestionsChooseVO>>> response) {
                if (response.isSuccessful()) {
                    List<PutQuestionsChooseVO> list = response.body().getResult();
                    if (list.size() == 0) {
                        visibleNoData();
                        dismissProgress();
                    } else
                        mTypeAdapter.setData(list);
                } else {
                    showTost(response.raw().message(), 1);
                    dismissProgress();
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                showTost(apiException.getMessage(), 1);

            }
        });

    }


}
